<template>
  <div>
    <tableComponents :pagination="{ currentPage, pageSize, total: list.all_num }" @handleCurrentChange="handleCurrentChange"
        @handleSizeChange="handleSizeChange" @sendUserSearch="getNewsSearch" :searchData="userSearchData" :isHaveExport="false" @toAdd="add">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list.data" v-loading="loading" stripe header-row-class-name="head_color" tooltip-effect="dark"
            style="width: 99.9%" :default-sort="{ prop: 'sort' }" @selection-change="handleSelectionChange">
          <el-table-column type="selection" class="tc" width="35"></el-table-column>
          <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" min-width="80">
            <template slot-scope="scope">
              <el-input @change="sortNew(scope.$index, scope.row)" v-model.number="scope.row.sort" autocomplete="off" size="small"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="标题" min-width="300">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column prop="name" label="是否置顶" min-width="80">
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.is_top" active-color="#1aad19" inactive-color="#ffffff" active-text="已置顶"
                  inactive-text="未置顶" active-value="1" @change="getTop($event, scope.row)" inactive-value="0"></el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="name" label="是否推荐" min-width="80">
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.is_recommend" active-color="#1aad19" inactive-color="#ffffff"
                  active-text="已推荐" inactive-text="未推荐" active-value="1" @change="getRecommend($event, scope.row)" inactive-value="0"></el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="name" label="是否显示" min-width="80">
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.show" active-color="#1aad19" inactive-color="#ffffff" active-text="已开启"
                  inactive-text="已关闭" active-value="1" @change="getIs($event, scope.row)" inactive-value="0"></el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="address" label="发布时间" min-width="120" show-overflow-tooltip>
            <template slot-scope="scope">{{
              scope.row.created_time | getTimeFormat
            }}</template>
          </el-table-column>

          <el-table-column label="操作" min-width="180">
            <template slot-scope="scope">
              <el-button :loading="$store.state.isLoading" type="primary" size="mini" @click="copyNews(scope.row.id)">复制</el-button>
              <el-button :loading="$store.state.isLoading" type="primary" size="mini" @click="updateNews(scope.row.id)">编辑</el-button>
              <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="deleteNew(scope.row.id)">删除</el-button>
              <el-button :loading="$store.state.isLoading" size="mini" type="warning" @click="$router.push('/index/news/news_comment?news_id='+scope.row.id)">评论</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteLises">删除</a>
      </template>
    </tableComponents>
    <el-dialog :close-on-click-modal="false" title="复制资讯" :modal-append-to-body="false" :visible.sync="copy_show" class="tl size_13" width="800px">
      <el-form ref="data" class="mar_top" label-width="150px">
        <el-form-item label="资讯分类选择：">
          <el-cascader :options="data" :props="optionProps" :show-all-levels="false" v-model="copy_form.cate_id" size="small" change-on-select
              placeholder="新闻分类" clearable></el-cascader>
        </el-form-item>
        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button :loading="$store.state.isLoading" size="medium" type="primary" @click.prevent="copy_news_btn()">复制</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import switchc from "../currency/Swich";
import AddNews from "./addnews";
import { newsSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data () {
    return {
      userSearchData: newsSearchData,
      optionProps: {
        value: "id",
        label: "title",
        children: "z"
      },
      data: this.$store.state.newsFeiLei || [],
      copy_form: {
        id: "",
        cate_id: Number
      },
      copy_show: false,
      loading: false,
      currentPage: 1,
      list: this.$store.state.newsList,
      pageSize: 10,
      multipleSelection: [],
      sort: "",
      searchAll: {}
    };
  },

  components: {
    switchc,
    AddNews
  },
  methods: {
    getNewFenLeiTree () {
      var params = {};
      params.parent_id = "0";
      this.$api.getNewsFenlei.getNewsList(params).then(res => {
        if (res.data.code == 200 && res.data.status == 1 && res.data.result) {
          this.data = res.data.result;
        }
      });
    },
    copy_news_btn () {
      var params = {};
      params.id = this.copy_form.id;
      params.cate_id = this.copy_form.cate_id[
        this.copy_form.cate_id.length - 1
      ];
      this.$api.getNews.copy_news(params).then(res => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess({ text: "复制成功" });
          var params = this.searchAll;
          params.page = this.$route.query.page || 1;
          params.page_size = this.pageSize;
          this.$store.dispatch("getNewsList", params);
          this.currentPage1 = Number(this.$route.query.page) || 1;
          this.copy_show = false;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    copyNews (id) {
      this.copy_form.id = id;
      this.copy_show = true;
    },
    add () {
      this.$store.commit("addbreadcrumbs", {
        title: "添加新闻",
        url: `/index/news/addnews`
      });
      this.$router.push("/index/news/addnews");
    },
    getNewsSearch (obj) {
      this.searchAll = obj;
      this.$router.push(this.$route.path + "?page=" + 1);
      this.$store.dispatch("getNewsList", obj);
    },
    getNewsAll (page, page_size) {
      this.loading = true;
      var _this = this;
      this.$router.push(this.$route.path + "?page=" + 1);
      this.$store.dispatch("getNewsList", { page, page_size });
      setTimeout(() => {
        _this.loading = false;
      }, 1000);
    },
    handleSizeChange (val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;

      this.$store.dispatch("getNewsList", params);
      // this.getNewsAll("",val)
      this.pageSize = val;
    },
    handleCurrentChange (val) {
      this.$router.push(this.$route.path + "?page=" + val);
    },
    updateNews (id) {
      let routeData = this.$router.resolve({
        path: "/index/news/addNews",
        query: { id: id }
      });
      window.open(routeData.href, "_blank");
    },
    deleteNew (id) {
      //查找文章
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id };
          this.$api.getNews.deleteNews(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll("", this.pageSize);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    getTop (val, row) {
      var params = {};
      params.id = row.id;
      params.is_top = val;
      this.updata(params);
    },
    getRecommend (val, row) {
      var params = {};
      params.id = row.id;
      params.is_recommend = val;
      this.updata(params);
    },
    getIs (val, row) {
      var params = {};
      params.id = row.id;
      params.show = val;
      this.updata(params);
    },
    deleteLises () {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            var data = { id_str: str };
            this.$api.getNews.deleteNewsAll(data).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这些信息。", "success");
                this.getNewsAll("", this.pageSize);
              } else {
                this.$swal("删除失败！", "出错啦！", "success");
              }
            });
          }
        });
      }
    },
    sortNew (index, row) {
      var params = {};
      params.id = row.id;
      params.sort = row.sort;
      this.updata(params);
    },
    updata (row) {
      var params = {};
      params = row;
      this.$api.getNews.updateNew(params).then(res => {
        if (res.data.status == 1 && res.data.code == 200) {
          //  this.$message.success('提交成功');
          if (params.sort) {
            var obj = {};
            obj.page = "";
            obj.page_size = this.pageSize;
            this.$store.dispatch("getNewsList", obj);
          }
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    sorta (a, b) {
      return b - a;
    }
  },
  created () {
    var params = this.searchAll;
    params.page = this.$route.query.page || 1;
    params.page_size = this.pageSize;
    this.$store.dispatch("getNewsList", params);
    this.currentPage1 = Number(this.$route.query.page) || 1;
    this.$store.dispatch("getNewsCate");
  },
  computed: {
    getNewsList () {
      return this.$store.state.newsList;
    }
  },
  watch: {
    getNewsList (data) {
      this.list = data;
    },
    $route () {
      var params = this.searchAll;
      params.page = this.$route.query.page || 1;
      params.page_size = this.pageSize;
      this.$store.dispatch("getNewsList", params);
      this.currentPage1 = Number(this.$route.query.page) || 1;
    }
  }
};
</script>
<style lang="less" scoped></style>
